package com.fjeld.server;

import com.fjeld.middleware.constants.PropertiesManager;
import com.fjeld.server.ssl.ServerStackLayer;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import javax.jms.JMSException;
import java.net.URL;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: fjeld
 * Date: 11/12/2011
 * Time: 11:37
 * <p/>
 * Abstract server. Will set up logger and load properties file.
 */
public class ServerStarter {

    public static String SERVER_NAME = "NOT_SET_YET";

    public static void main(String[] args) throws ClassNotFoundException, JMSException {
        configureLogger();
        PropertiesManager.getProperties();
        ServerStarter ss = new ServerStarter(args);
    }

    private static void configureLogger() {
        ClassLoader loader = ServerStarter.class.getClassLoader();

        String log4jPropertiesFilename = "/com/log4j.properties";

        if (loader != null) {
            URL propertiesFile = ServerStarter.class.getResource(log4jPropertiesFilename);
            PropertyConfigurator.configure(propertiesFile);
        }
    }

    private Logger logger = Logger.getLogger(ServerStarter.class);
    private ServerStarter(String[] args)
    {
        if (null == args)
        {
            logger.fatal("Cannot start server: no server stack program parameter was passed in. " +
                    "This application needs this to decided what server properties to load.");
            System.exit(1);
        } else if (args.length != 1)
        {
            logger.fatal("Bad arguments length, should be 1 but is: " + args.length);
            System.exit(1);
        }

        SERVER_NAME = args[0];
        logger.info("Starting initialisation of server stack: " + SERVER_NAME);
        List<String> sslNameList = PropertiesManager.getProperties().getPropertyAsList(SERVER_NAME);
        logger.info("Starting these server stack layers: " + sslNameList.toString());
        for (String sslName : sslNameList)
        {
            ServerStackLayer ssl = ServerStackLoader.loadServerStackLayer(SERVER_NAME, sslName);
        }
    }
}
